#include "mlog.h"
#include <time.h>

int mlog( char *logname, char *filename, int line, char *fmt, ... )
{
	char buf1[512];
	char buf2[256];
	va_list args;
	int i, j;
	FILE *logfile;
	time_t tm;
	struct tm *timeinfo;
	char dt[30];
	
	assert( filename != NULL );
	assert( logname != NULL );
	/*get current local time*/
	memset( &tm, 0x00, sizeof(time_t) );
	memset (&timeinfo, 0x00, sizeof(struct tm) );
	memset( dt, 0x00, sizeof(dt) );
	time(&tm);
	timeinfo=localtime(&tm);
	/*format datetime like year/mon/date h:m:s*/
	sprintf( dt, "%d/%d/%d %d:%d:%d", 1900+timeinfo->tm_year,timeinfo->tm_mon,timeinfo->tm_mday,
	                                  timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec );
	logfile = fopen( logname, "a" );
	
  /*write log infomation in logfile*/
	if( logfile != NULL )
	{
		  memset( buf1, 0x00, sizeof(buf1) );
		  memset (buf2, 0x00, sizeof(buf2) );
		  
		  /*infomation about date, time, sourcefile name, log line*/
		  i=sprintf( buf1, "%s %s:%d->", dt, filename, line );
		  
		  /*user log infomation*/
		  args = (va_list) ( (char*) (&fmt)+4);
		  j=vsprintf( buf2, fmt, args );
		  strcat( buf1, buf2 );
		  fprintf( logfile, "%s\n", buf1 );

	}
	
	/*close file handle*/
	fclose(logfile);
	return i;
}